package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Equipment;
import com.woniu.entity.Ward;
import com.woniu.mapper.EquipmentMapper;
import com.woniu.service.EquipmentService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Resource
    private EquipmentMapper equipmentMapper;
    @Override
    public ResponseUtil findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Equipment> equipmentList = equipmentMapper.findAll();
        PageInfo pageInfo = new PageInfo(equipmentList);
        return equipmentList != null ?ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateEquipment(Integer id) {
        int rows = equipmentMapper.updateEquipment(id);
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil insertSelective(Equipment record) {
        int rows = equipmentMapper.insertSelective(record);
//        return rows == 1
//                ? ResponseUtil.get(ResponseEnum.OK, record)
//                : ResponseUtil.get(ResponseEnum.FAIL);
        if (rows != 1){
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
        if (record.getPurchaseDate() != null){
            LocalDate purchaseDate = record.getPurchaseDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate nextMaintenance = purchaseDate.plusDays(14);
            record.setNextMaintenanceDate(Date.from(nextMaintenance.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            record.setMaintenanceStatus(2);

            equipmentMapper.updateByPrimaryKeySelective(record);

        }
        return ResponseUtil.get(ResponseEnum.OK,record);
    }

    @Override
    public ResponseUtil updateByPrimaryKeySelective(Equipment record) {
        int rows = equipmentMapper.updateByPrimaryKeySelective(record);
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil selectByCondition(String equipName, String roomName, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Equipment> equipmentList = equipmentMapper.selectByCondition(equipName,roomName,status);
        PageInfo pageInfo = new PageInfo(equipmentList);
        return equipmentList!=null ? ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil confirmMaintenance(Integer id) {
        Equipment equipment = equipmentMapper.selectByPrimaryKey(id);
        if (equipment == null) {
            return ResponseUtil.get(ResponseEnum.FAIL,"设备不存在");
        }
        equipment.setLastMaintenanceDate(new java.util.Date());

        // 设置下次维护日期为当前日期+14天
        LocalDate nextDate = LocalDate.now().plusDays(14);
        equipment.setNextMaintenanceDate(Date.from(nextDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));

        // 设置维护状态为2已维护
        equipment.setMaintenanceStatus(2);

        int rows = equipmentMapper.updateByPrimaryKeySelective(equipment);
        return rows == 1 ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
